//
//  HMComposeToolBar.swift
//  MicroBlog
//
//  Created by Apple on 16/9/28.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
enum HMComposeToolBarButtonType: Int {
    //图片
    case picture
    // @
    case mention
    // #
    case trend
    //表情
    case emotion
    // 加号
    case add
}
//只是一个容器控件,不具备渲染效果
class HMComposeToolBar: UIStackView {
    
    var callBack: ((HMComposeToolBarButtonType) -> ())?
    //表情按钮
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //水平布局
        axis = .horizontal
        //子控件填充方式
        distribution = .fillEqually
        backgroundColor = RandomColor()
        addChildBUtton(imageName: "compose_toolbar_picture", type: .picture)
        addChildBUtton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildBUtton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildBUtton(imageName: "compose_emoticonbutton_background", type: .emotion)
        addChildBUtton(imageName: "compose_add_background", type: .add)
    }
    
    //添加子控件
    //忽略返回值的处理,不关系返回的结果
    @discardableResult
    private func addChildBUtton(imageName: String, type: HMComposeToolBarButtonType) -> UIButton{
        let button = UIButton()
        //枚举值设置为tag
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named: "\(imageName)"), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        //去掉高亮效果
        button.adjustsImageWhenHighlighted = false
        //添加使用
        addArrangedSubview(button)
        
        return button
    }
    
    //点击事件
    @objc private func btnAction(btn: UIButton) {
        let type = HMComposeToolBarButtonType(rawValue: btn.tag)!
        callBack?(type)
    }
    
    //根据表情键盘类型显示不同的icon
    func showIcon(isEmoticon: Bool) {
        //如果是表情键盘,显示系统icon
        if isEmoticon {
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        } else {
        //如果是系统键盘,显示表情icon
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
    
}
